package cn.virens.web.components.beetl.fun;

import java.text.MessageFormat;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

public class StringUtil extends org.beetl.ext.fn.StringUtil {

	public String[] split(String str) {
		return split(str, ",");
	}

	@Override
	public String[] split(String str, String key) {
		return StrUtil.split(str, key);
	}

	@Override
	public String trim(String str) {
		return StrUtil.trim(str);
	}

	@Override
	public boolean startWith(String str, String key) {
		return StrUtil.startWith(str, key);
	}

	@Override
	public boolean endWith(String str, String key) {
		return StrUtil.endWith(str, key);
	}

	@Override
	public int length(String str) {
		return StrUtil.totalLength(str);
	}

	@Override
	public String subStringTo(String str, int start, int end) {
		return StrUtil.sub(str, start, end);
	}

	@Override
	public String subString(String str, int start) {
		return StrUtil.subSuf(str, start);
	}

	@Override

	public boolean contain(String str, String key) {
		return StrUtil.containsAny(str, key);
	}

	@Override
	public String toUpperCase(String str) {
		if (str != null && str.isEmpty() == false) {
			return str.toUpperCase();
		} else {
			return str;
		}
	}

	@Override
	public String toLowerCase(String str) {
		if (str != null && str.isEmpty() == false) {
			return str.toLowerCase();
		} else {
			return str;
		}
	}

	@Override
	public String replace(String str, String oldValue, String newValue) {
		return StrUtil.replace(str, oldValue, newValue);
	}

	@Override
	public String format(String str, Object[] args) {
		if (str != null) {
			return MessageFormat.format(str, args);
		} else {
			return "";
		}
	}

	@Override
	public String formatDate(Date o, String args) {
		if (o == null) { return ""; }

		if (args == null) {
			return DateUtil.formatDateTime(o);
		} else {
			return DateUtil.format(o, args);
		}
	}

	@Override
	public int index(String str, String search) {
		return StrUtil.indexOf(str, search, 0, false);
	}

	@Override
	public int lastIndex(String str, String search) {
		return StrUtil.lastIndexOf(str, search, 0, false);
	}
}
